package com.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.util.HbnUtil;

import entities.*;

/**
 * Servlet implementation class AddProduct
 */
@WebServlet("/AddProduct")
public class AddProduct extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddProduct() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Session session = HbnUtil.getSession();
		
		HttpSession se = request.getSession(true);
		Product p = new Product();
		Seller seller = new Seller();
		Transaction tran = session.beginTransaction();
		seller = (Seller) session.createQuery("from Seller where userid =" +se.getAttribute("Userid")).uniqueResult();
		p.setName(request.getParameter("Productname"));
		p.setDescription(request.getParameter("Description"));
		p.setPrice(Double.parseDouble(request.getParameter("Price")));
		p.setDiscount(Double.parseDouble(request.getParameter("Discount")));
		//p.setImgpath(request.getParameter("Imgpath"));
		p.setSeller(seller);
		session.save(p);
		tran.commit();
		session.close();
		Product pro = (Product)session.createQuery("from Product where productname='"+ p.getName()+"'").uniqueResult();
		
		if(pro != null)
		{
			response.sendRedirect("productedit.jsp");
		}
	}

}
